package com.itheima;

import java.io.*;
import java.net.Socket;

/**
 * @auther Macrydy
 * @date 2020/7/10 14:10
 */
public class TcpClient implements Runnable {
    private Socket socket;

    public TcpClient(Socket s) {
        this.socket=s;
    }

    @Override
    public void run() {
        OutputStream os = null;
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\1.jpg"));

            //获取输出流，写数据
            //OutputStream getOutputStream() 返回此套接字的输出流
            os = socket.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);
            int i;
            while ((i=bis.read())!=-1){
                os.write(i);
            }
            bos.flush();
            socket.shutdownOutput();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String len;
            while ((len=br.readLine())!=null){
                System.out.println(len);
            }
            br.close();

            //释放资源
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                os.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
